﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IssueTrackerTest.IssueTrackerService;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for IssueTrackerServiceTest
    /// </summary>
    [TestClass]
    public class IssueTrackerServiceTesting
    {

        private IssueTrackerService.IssueTrackerServiceClient client;

        public IssueTrackerServiceTesting()
        {
            client = new IssueTrackerService.IssueTrackerServiceClient();
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // 
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestInitialize]
        public void ProjectTestInitialize()
        {
            
            #region Project
            if (client.ListProjects().ToList() == null)
            {
                IssueTrackerService.Project project = new IssueTrackerService.Project();

                project.Name = "Mock Test Project";
                project.Description = "Mock Test Project";
                project.CreatorId = "patrickyong";
                project.ProjectManagerId = "patrickyong";

                IssueTrackerService.Project result = client.CreateProject(project);

                projectId = result.Id;
            }
            #endregion

            #region ProjectMember
            if (client.ListProjectMembers().ToList() == null)
            {
                IssueTrackerService.ProjectMember projectMember = new IssueTrackerService.ProjectMember();


                projectMember.ProjectId = projectId;
                projectMember.UserId = "testuser";
                projectMember.CreatorId = "testuser";

                client.CreateProjectMember(projectMember);

            }
            #endregion

            #region ProjectCustomType
            if (client.ListProjectCustomTypes().ToList() == null)
            {
                IssueTrackerService.ProjectCustomType projectCustomType = new IssueTrackerService.ProjectCustomType();

                projectCustomType.ProjectId = projectId;
                projectCustomType.Description = "Test Data";
                projectCustomType.DataType = "string";
                projectCustomType.IsRequired = false;
                projectCustomType.CreatorId = "testuser";
                projectCustomType.Name = "test data";

                int projectCustomTypeId = client.CreateProjectCustomType(projectCustomType).Id;
            }
            #endregion

            #region ProjectCustomTypeColumn
            if (client.ListProjectCustomTypeColumns().ToList() == null)
            {
                IssueTrackerService.ProjectCustomTypeColumn projectCustomTypeColumn = new IssueTrackerService.ProjectCustomTypeColumn();

                projectCustomTypeColumn.Name = "test data";
                projectCustomTypeColumn.CreatorId = "testuser";
                projectCustomTypeColumn.ProjectCustomTypeId = projectCustomTypeId;

                int projectCustomTypeColumnId = client.CreateProjectCustomTypeColumn(projectCustomTypeColumn).Id;
            }
            #endregion

            #region Issue

            System.Guid issueId = System.Guid.NewGuid();

            if (client.ListIssues().ToList() == null)
            {
                IssueTrackerService.Issue issue = new IssueTrackerService.Issue();

                issue.Id = System.Guid.NewGuid();
                issue.ProjectId = projectId;
                issue.CreatorId = "testuser";
                issue.Description = "test data";
                issue.Title = "test title";

                issueId = client.CreateIssue(issue).Id;

            }
            #endregion

            #region Issue Attachment
            if (client.ListIssueAttachments().ToList() == null)
            {
                IssueTrackerService.IssueAttachment attachment = new IssueTrackerService.IssueAttachment();

                attachment.IssueId = issueId;
                attachment.ContentType = "mime/text";
                attachment.Filename = "testuser";
                attachment.CreatorId = "testuser";

                attachment.Attachment = GetFileFromResource("sample");

                client.CreateIssueAttachment(attachment);
            }
            #endregion

            #region Issue Custom Type Value
            if (client.ListIssueCustomTypeValues().ToList() == null)
            {
                IssueTrackerService.IssueCustomTypeValue customTypeValue = new IssueTrackerService.IssueCustomTypeValue();

                customTypeValue.IssueId = issueId;
                customTypeValue.CreatorId = "testuser";
                customTypeValue.Value = "test data";
                customTypeValue.ProjectCustomTypeId = projectCustomTypeId;

                client.CreateIssueCustomTypeValue(customTypeValue);
            }
            #endregion

            #region Issue Task
            System.Guid taskId = System.Guid.NewGuid();
            System.Guid actId = System.Guid.NewGuid();
            System.Guid wfTaskId = System.Guid.NewGuid();

            if (client.ListIssueTasks().ToList() == null)
            {
                IssueTrackerService.IssueTask task = new IssueTrackerService.IssueTask();

                task.IssueId = issueId;
                task.WfActivityId = System.Guid.NewGuid();
                task.WfTaskId = System.Guid.NewGuid();
                task.HolderId = "testuser";
                task.CreatorId = "testuser";

                task = client.CreateIssueTask(task);

                taskId = task.Id;
                actId = task.WfActivityId;
                wfTaskId = task.WfTaskId;
            }
                
            #endregion

            #region Issue WF History
            if (client.ListIssueWfHistories().ToList() == null)
            {
                IssueTrackerService.IssueWfHistory history = new IssueTrackerService.IssueWfHistory();


                history.IssueId = taskId;
                history.WfActivityId = actId;
                history.WfTaskId = wfTaskId;
                history.CreatorId = "testuser";

                client.CreateIssueWfHistory(history);
            }
#endregion

        }

        #region Testing ProjectBO

        private int projectId;

        
        public void TestAddProject()
        {
            IssueTrackerService.Project project = new IssueTrackerService.Project();

            project.Name = "Mock Test Project";
            project.Description = "Mock Test Project";
            project.CreatorId = "patrickyong";
            project.ProjectManagerId = "patrickyong";

            IssueTrackerService.Project result = client.CreateProject(project);

            projectId = result.Id;

            Assert.IsNotNull(projectId);
        }


        
        public void TestUpdateProject()
        {
            List<IssueTrackerService.Project> list = client.ListProjects().ToList();

            if (list != null)
            {
                IssueTrackerService.Project project = list.First();

                project.Description = "Mock Test Updated";

                IssueTrackerService.Project result = client.UpdateProject(project);

                Assert.AreEqual("Mock Test Updated", result.Description);
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteProject()
        {
            //int projectId;
            List<IssueTrackerService.Project> list = client.ListProjects().ToList();

            if (list != null)
            {
                IssueTrackerService.Project project = list.First();

                projectId = project.Id;

                client.DeleteProject(project);

                var checkProject = client.GetProject(projectId);

                Assert.IsNull(checkProject);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion
  
        #region Testing ProjectMemberBO

        private int projectMemberId;

        //
        //public void ProjectMemberTestInitialize()
        //{
            
        //}

        
        public void TestAddProjectMember()
        {
            IssueTrackerService.ProjectMember entity = new IssueTrackerService.ProjectMember();

            List<IssueTrackerService.Project> list = client.ListProjects().ToList();

            if (list != null)
            {
                IssueTrackerService.Project project = list.First();

                entity.ProjectId = project.Id;
                entity.UserId = "testuser";
                entity.CreatorId = "testuser";

                IssueTrackerService.ProjectMember result = client.CreateProjectMember(entity);

                projectMemberId = result.Id;

                Assert.IsNotNull(projectMemberId);

            }
            else
            {
                Assert.Fail("Project table does not contain data.");
            }
        }


        
        public void TestUpdateProjectMember()
        {
            List<IssueTrackerService.ProjectMember> list = client.ListProjectMembers().ToList();

            if (list != null)
            {
                IssueTrackerService.ProjectMember entity = list.First();

                entity.UserId = "newUserId";

                IssueTrackerService.ProjectMember result = client.UpdateProjectMember(entity);

                Assert.AreEqual(result.UserId, "newUserId");
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteProjectMember()
        {
            //int projectMemberId;
            List<IssueTrackerService.ProjectMember> list = client.ListProjectMembers().ToList();

            if (list != null)
            {
                IssueTrackerService.ProjectMember entity = list.First();

                projectMemberId = entity.Id;

                client.DeleteProjectMember(entity);

                var checkProjectMember = client.GetProjectMember(projectMemberId);

                Assert.IsNull(checkProjectMember);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion
        
        #region Testing ProjectCustomTypeBO

        private int projectCustomTypeId;

        //
        //public void ProjectCustomTypeTestInitialize()
        //{
            
        //}

        
        public void TestAddProjectCustomType()
        {
            IssueTrackerService.ProjectCustomType entity = new IssueTrackerService.ProjectCustomType();

            List<IssueTrackerService.Project> list = client.ListProjects().ToList();

            if (list != null)
            {
                IssueTrackerService.Project project = list.First();

                entity.ProjectId = project.Id;
                entity.Description = "Test Data";
                entity.DataType = "string";
                entity.IsRequired = false;
                entity.CreatorId = "testuser";
                entity.Name = "test data";

                IssueTrackerService.ProjectCustomType result = client.CreateProjectCustomType(entity);

                projectMemberId = result.Id;

                Assert.IsNotNull(projectMemberId);

            }
            else
            {
                Assert.Fail("Project table is empty");
            }
        }


        
        public void TestUpdateProjectCustomType()
        {
            List<IssueTrackerService.ProjectCustomType> list = client.ListProjectCustomTypes().ToList();

            if (list != null)
            {
                IssueTrackerService.ProjectCustomType entity = list.First();

                entity.Description = "modified for unit test";

                IssueTrackerService.ProjectCustomType result = client.UpdateProjectCustomType(entity);

                Assert.AreEqual(result.Description, "modified for unit test");
            }
            else
            {
                Assert.Fail("Project Custom Type table is empty");
            }
        }

        
        public void TestDeleteProjectCustomType()
        {
            //int projectCustomTypeId;
            List<IssueTrackerService.ProjectCustomType> list = client.ListProjectCustomTypes().ToList();

            if (list != null)
            {
                IssueTrackerService.ProjectCustomType entity = list.First();

                projectCustomTypeId = entity.Id;

                client.DeleteProjectCustomType(entity);

                var checkProjectCustomType = client.GetProjectCustomType(projectCustomTypeId);

                Assert.IsNull(checkProjectCustomType);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion

        #region Testing ProjectCustomTypeColumnBO

        private int projectCustomTypeColumnId;

        //
        //public void ProjectCustomTypeColumnTestInitialize()
        //{
            
        //}

        
        public void TestAddProjectCustomTypeColumn()
        {
            IssueTrackerService.ProjectCustomTypeColumn entity = new IssueTrackerService.ProjectCustomTypeColumn();

            List<IssueTrackerService.ProjectCustomType> list = client.ListProjectCustomTypes().ToList();

            if (list != null)
            {
                IssueTrackerService.ProjectCustomType projectCustomType = list.First();

                entity.Name = "test";
                entity.CreatorId = "testuser";
                entity.ProjectCustomTypeId = projectCustomType.Id;

                IssueTrackerService.ProjectCustomTypeColumn result = client.CreateProjectCustomTypeColumn(entity);

                projectCustomTypeColumnId = result.Id;

                Assert.IsNotNull(projectCustomTypeColumnId);
            }
            else
            {
                Assert.Fail();
            }
        }


        
        public void TestUpdateProjectCustomTypeColumn()
        {
            List<IssueTrackerService.ProjectCustomTypeColumn> list = client.ListProjectCustomTypeColumns().ToList();

            if (list != null)
            {
                IssueTrackerService.ProjectCustomTypeColumn entity = list.First();

                //TODO: enter test data for ProjectCustomTypeColumn
                entity.Name = "Modified for testing";

                IssueTrackerService.ProjectCustomTypeColumn result = client.UpdateProjectCustomTypeColumn(entity);

                Assert.AreEqual(result.Name, "Modified for testing");
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteProjectCustomTypeColumn()
        {
            //int projectCustomTypeColumnId;
            List<IssueTrackerService.ProjectCustomTypeColumn> list = client.ListProjectCustomTypeColumns().ToList();

            if (list != null)
            {
                IssueTrackerService.ProjectCustomTypeColumn entity = list.First();

                projectCustomTypeColumnId = entity.Id;

                client.DeleteProjectCustomTypeColumn(entity);

                var checkProjectCustomTypeColumn = client.GetProjectCustomTypeColumn(projectCustomTypeColumnId);

                Assert.IsNull(checkProjectCustomTypeColumn);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion

        #region Testing IssueBO

        private System.Guid issueId;

        //
        //public void IssueTestInitialize()
        //{
            
        //}

        
        public void TestAddIssue()
        {
            IssueTrackerService.Issue entity = new IssueTrackerService.Issue();

            List<IssueTrackerService.Project> list = client.ListProjects().ToList();

            if (list != null)
            {
                IssueTrackerService.Project project = list.First();

                entity.ProjectId = project.Id;
                entity.CreatorId = "testuser";
                entity.Description = "test data";
                entity.Title = "test title";

                IssueTrackerService.Issue result = client.CreateIssue(entity);

                issueId = result.Id;

                Assert.IsNotNull(issueId);
            }
            else
            {
                Assert.Fail();
            }
        }


        
        public void TestUpdateIssue()
        {
            List<IssueTrackerService.Issue> list = client.ListIssues().ToList();

            if (list != null)
            {
                IssueTrackerService.Issue entity = list.First();

                entity.Title = "modified";

                IssueTrackerService.Issue result = client.UpdateIssue(entity);

                Assert.AreEqual(result.Title, "modified");
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteIssue()
        {
            //System.Guid issueId;
            List<IssueTrackerService.Issue> list = client.ListIssues().ToList();

            if (list != null)
            {
                IssueTrackerService.Issue entity = list.First();

                issueId = entity.Id;

                client.DeleteIssue(entity);

                var checkIssue = client.GetIssue(issueId);

                Assert.IsNotNull(checkIssue);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion
        
        #region Testing IssueAttachmentBO

        private System.Guid issueAttachmentId;

        //
        //public void IssueAttachmentTestInitialize()
        //{
            
        //}

        private Binary GetFileFromResource(string resourceName)
        {
            var myAssembly = this.GetType().Assembly;

            var resourceManager = new System.Resources.ResourceManager(this.GetType());

            Binary thisFile = (Binary)resourceManager.GetObject(resourceName);

            return thisFile;

        }

        //
        //public void TestAddIssueAttachment()
        //{
        //    IssueTrackerService.IssueAttachment entity = new IssueTrackerService.IssueAttachment();

        //    //TODO: enter test data for IssueAttachment

        //    IssueTrackerService.IssueAttachment result = client.CreateIssueAttachment(entity);

        //    issueAttachmentId = result.Id;

        //    //TODO: insert you assertion
        //}


        
        public void TestUpdateIssueAttachment()
        {
            List<IssueTrackerService.IssueAttachment> list = client.ListIssueAttachments().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueAttachment entity = list.First();

                entity.Filename = "modified";


                IssueTrackerService.IssueAttachment result = client.UpdateIssueAttachment(entity);

                Assert.AreEqual(result.Filename, "modified");
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteIssueAttachment()
        {
            //System.Guid issueAttachmentId;
            List<IssueTrackerService.IssueAttachment> list = client.ListIssueAttachments().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueAttachment entity = list.First();

                issueAttachmentId = entity.Id;

                client.DeleteIssueAttachment(entity);

                var checkIssueAttachment = client.GetIssueAttachment(issueAttachmentId);

                Assert.IsNull(checkIssueAttachment);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion
        
        #region Testing IssueCustomTypeValueBO

        private System.Guid issueCustomTypeValueId;

        //
        //public void IssueCustomTypeValueTestInitialize()
        //{
            
        //}

        //
        //public void TestAddIssueCustomTypeValue()
        //{
        //    IssueTrackerService.IssueCustomTypeValue entity = new IssueTrackerService.IssueCustomTypeValue();

            

        //    IssueTrackerService.IssueCustomTypeValue result = client.CreateIssueCustomTypeValue(entity);

        //    issueCustomTypeValueId = result.Id;

            
        //}


        
        public void TestUpdateIssueCustomTypeValue()
        {
            List<IssueTrackerService.IssueCustomTypeValue> list = client.ListIssueCustomTypeValues().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueCustomTypeValue entity = list.First();

                entity.Value = "modified";

                IssueTrackerService.IssueCustomTypeValue result = client.UpdateIssueCustomTypeValue(entity);

                Assert.AreEqual(result.Value, "modified");
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteIssueCustomTypeValue()
        {
            //System.Guid issueCustomTypeValueId;
            List<IssueTrackerService.IssueCustomTypeValue> list = client.ListIssueCustomTypeValues().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueCustomTypeValue entity = list.First();

                issueCustomTypeValueId = entity.Id;

                client.DeleteIssueCustomTypeValue(entity);

                var checkIssueCustomTypeValue = client.GetIssueCustomTypeValue(issueCustomTypeValueId);

                Assert.IsNull(checkIssueCustomTypeValue);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion

        #region Testing IssueTaskBO

        private System.Guid issueTaskId;

        //
        //public void IssueTaskTestInitialize()
        //{
            
        //}

        //
        //public void TestAddIssueTask()
        //{
        //    IssueTrackerService.IssueTask entity = new IssueTrackerService.IssueTask();

        //    //TODO: enter test data for IssueTask

        //    IssueTrackerService.IssueTask result = client.CreateIssueTask(entity);

        //    issueTaskId = result.Id;

        //    //TODO: insert you assertion
        //}


        
        public void TestUpdateIssueTask()
        {
            List<IssueTrackerService.IssueTask> list = client.ListIssueTasks().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueTask entity = list.First();

                entity.HolderId = "modified";

                IssueTrackerService.IssueTask result = client.UpdateIssueTask(entity);

                Assert.AreEqual(result.HolderId, "modified");
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteIssueTask()
        {
            //System.Guid issueTaskId;
            List<IssueTrackerService.IssueTask> list = client.ListIssueTasks().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueTask entity = list.First();

                issueTaskId = entity.Id;

                client.DeleteIssueTask(entity);

                var checkIssueTask = client.GetIssueTask(issueTaskId);

                Assert.IsNull(checkIssueTask);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion

        #region Testing IssueWfHistoryBO

        private System.Guid issueWfHistoryId;

        //
        //public void IssueWfHistoryTestInitialize()
        //{
            
        //}

        //
        //public void TestAddIssueWfHistory()
        //{
        //    IssueTrackerService.IssueWfHistory entity = new IssueTrackerService.IssueWfHistory();

        //    //TODO: enter test data for IssueWfHistory

        //    IssueTrackerService.IssueWfHistory result = client.CreateIssueWfHistory(entity);

        //    issueWfHistoryId = result.Id;

        //    //TODO: insert you assertion
        //}


        
        public void TestUpdateIssueWfHistory()
        {
            List<IssueTrackerService.IssueWfHistory> list = client.ListIssueWfHistories().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueWfHistory entity = list.First();

                entity.CreatorId = "modified";

                IssueTrackerService.IssueWfHistory result = client.UpdateIssueWfHistory(entity);

                Assert.AreEqual(result.CreatorId, "modified");
            }
            else
            {
                Assert.Fail();
            }
        }

        
        public void TestDeleteIssueWfHistory()
        {
            //System.Guid issueWfHistoryId;
            List<IssueTrackerService.IssueWfHistory> list = client.ListIssueWfHistories().ToList();

            if (list != null)
            {
                IssueTrackerService.IssueWfHistory entity = list.First();

                issueWfHistoryId = entity.Id;

                client.DeleteIssueWfHistory(entity);

                var checkIssueWfHistory = client.GetIssueWfHistory(issueWfHistoryId);

                Assert.IsNull(checkIssueWfHistory);
            }
            else
            {
                Assert.Fail();
            }
        }

        #endregion
      
    }
}
